﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using BabyFood.Data;
using BabyFood.Entities;
using System.Data.SqlClient;
using System.Configuration;

public partial class Admin_LienHe_HTLienHe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Context.User.Identity.IsAuthenticated)
        {
            FormsAuthentication.RedirectToLoginPage();
        }
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                try
                {
                    H_CatalogId.Value = Request.QueryString["id"].ToString();
                    LienHe entLienHe = DataRepository.LienHeProvider.GetById(new Guid(H_CatalogId.Value));
                    if (entLienHe != null)
                    {
                        txtHoTen.Text = entLienHe.HoTen;
                        txtThuDienTu.Text = entLienHe.ThuDienTu;
                        txtTieuDe.Text = entLienHe.TieuDe;
                        txtNoiDung.Text = entLienHe.NoiDung;
                        string query;
                        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
                        conn.Open();
                        query = "UPDATE LienHe SET TrangThai = 1 where Id = '" + Request.QueryString["Id"].ToString() + "'";
                        SqlCommand sqlCommand = new SqlCommand();
                        sqlCommand.Connection = conn;
                        sqlCommand.CommandText = query;
                        sqlCommand.ExecuteNonQuery();
                        conn.Close();
                    }
                }
                catch (Exception ex)
                {
                    Common.WriteLog("HTLienHe.aspx.cs", "Page_Load()", ex.ToString());
                    string strScript = "<script>parent.parent.GB_hide();</script>";
                    this.RegisterClientScriptBlock("strScript", strScript);
                }
            }
        }
    }

    protected void cmdDelete_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            bool updateStatus = DataRepository.LienHeProvider.Delete(new Guid(H_CatalogId.Value));
            if (updateStatus)
                WebMessage.Show("Xóa Liên hệ thành công!");
            else
                WebMessage.Show("Xóa Liên hệ thất bại!");

            string strScript = "<script>parent.parent.GB_hide();</script>";
            this.RegisterClientScriptBlock("strScript", strScript);
        }
        catch (Exception ex)
        {
            Common.WriteLog("HTLienHe.aspx.cs", "cmdDelete_Click()", ex.ToString());
            WebMessage.Show("Xóa Liên hệ thất bại!");
        }
    }
    protected void btTraLoi_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            TraLoi ent_TraLoi = new TraLoi();
            ent_TraLoi.Id = Convert.ToInt32(H_CatalogIdTraLoi.Value);
            ent_TraLoi.IdLienHe = new Guid(H_CatalogId.Value);
            ent_TraLoi.IdNguoiDung = Convert.ToInt32(Common.GetKeyUserInfo("Id"));
            ent_TraLoi.TraLoi = Server.HtmlEncode(txtNoiDungTraLoi.Text);
            ent_TraLoi.TrangThai = chkTrangThai.Checked;
            ent_TraLoi.NgayCapNhat = Common.GetServerDateTime();

            if (ent_TraLoi.Id == 0)
            {
                ent_TraLoi.Id = 0;
                updateStatus = DataRepository.TraLoiProvider.Insert(ent_TraLoi);
            }
            else
            {
                ent_TraLoi.EntityState = EntityState.Changed;
                updateStatus = DataRepository.TraLoiProvider.Update(ent_TraLoi);
            }
            if (updateStatus)
            {
                txtNoiDungTraLoi.Text = "";
            }
            else
                WebMessage.Show("Nội dung trả lời đã được gửi đi!");
        }
        catch (Exception)
        {
            WebMessage.Show("Nội dung trả lời thất bại!");
        }
    }
    protected void btXoa_Click(object sender, ImageClickEventArgs e)
    {
        txtNoiDungTraLoi.Text = "";
    }
}
